/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JDialogAgregarCamara.java
 *
 * Created on 20/11/2012, 18:22:55
 */

package hotelinteligente.vista;

import hotelinteligente.Dispositivos;
import hotelinteligente.controladores.Controlador;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JComboBox;

/**
 *
 * @author vero
 */
public class JDialogActivarDispositivo extends javax.swing.JDialog implements ActionListener {

    private mainJFrame main;

    /** Creates new form JDialogAgregarCamara */
    public JDialogActivarDispositivo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.main= (mainJFrame) parent;
        initComponents();
        initDispositivosDesactivados();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        JComboPisos = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        JComboPuertas = new javax.swing.JComboBox();
        JComboBoxDispositivos = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jButtonAceptarAgregarDispositivo = new javax.swing.JButton();
        jButtonCancelarAgregarDispositivo = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Activar Dispositivo");
        setResizable(false);

        jLabel1.setText("Piso:");

        JComboPisos.setMaximumRowCount(1);
        JComboPisos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1" }));

        jLabel2.setText("Puerta:");

        JComboPuertas.setMaximumRowCount(2);
        JComboPuertas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2" }));

        JComboBoxDispositivos.setMaximumRowCount(4);

        jLabel3.setText("Dispositivo:");

        jButtonAceptarAgregarDispositivo.setText("Aceptar");
        jButtonAceptarAgregarDispositivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAceptarAgregarDispositivoActionPerformed(evt);
            }
        });

        jButtonCancelarAgregarDispositivo.setText("Cancelar");
        jButtonCancelarAgregarDispositivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarAgregarDispositivoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(JComboPisos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(JComboBoxDispositivos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(JComboPuertas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(257, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButtonCancelarAgregarDispositivo)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonAceptarAgregarDispositivo)
                        .addGap(9, 9, 9))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(JComboBoxDispositivos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(JComboPisos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(JComboPuertas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonCancelarAgregarDispositivo)
                    .addComponent(jButtonAceptarAgregarDispositivo))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCancelarAgregarDispositivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarAgregarDispositivoActionPerformed
        setVisible(false);
    }//GEN-LAST:event_jButtonCancelarAgregarDispositivoActionPerformed

    private void jButtonAceptarAgregarDispositivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAceptarAgregarDispositivoActionPerformed
        String dispositivo= (String) JComboBoxDispositivos.getSelectedItem();
        dispositivo= dispositivo.replace(" ", "");
        String puerta= (String) JComboPuertas.getSelectedItem();
        String id= dispositivo + puerta;
        Controlador.getInstancia().activarDispositivo(id);
        setVisible(false);
    }//GEN-LAST:event_jButtonAceptarAgregarDispositivoActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JDialogActivarDispositivo dialog = new JDialogActivarDispositivo(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox JComboBoxDispositivos;
    private javax.swing.JComboBox JComboPisos;
    private javax.swing.JComboBox JComboPuertas;
    private javax.swing.JButton jButtonAceptarAgregarDispositivo;
    private javax.swing.JButton jButtonCancelarAgregarDispositivo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration//GEN-END:variables

    private void initDispositivosDesactivados() {
        ArrayList<String> disp= new ArrayList<String>();
        for(int i= 0; i < Dispositivos.dispositivos.length; i++) {
            DispositivoDibujable d1= main.getPiso().getDispositivoDibujable(Dispositivos.dispositivos[i].replace(" ", "")+1);
            DispositivoDibujable d2= main.getPiso().getDispositivoDibujable(Dispositivos.dispositivos[i].replace(" ", "")+2);
            if(!d1.getDibujar() || !d2.getDibujar())
                disp.add(Dispositivos.dispositivos[i]);    
        }

        String[] dispositivos_no_activos= new String[disp.size()];
        for(int i= 0; i < disp.size(); i++)
            dispositivos_no_activos[i]= disp.get(i);

        JComboBoxDispositivos.setModel(new javax.swing.DefaultComboBoxModel(dispositivos_no_activos));
        JComboBoxDispositivos.addActionListener(this);
        updatePisos((String) JComboBoxDispositivos.getSelectedItem());
    }

    public void actionPerformed(ActionEvent ae) {
        JComboBox cb= (JComboBox) ae.getSource();
        String dispositivo= (String)cb.getSelectedItem();
        updatePisos(dispositivo);
    }

    private void updatePisos(String dispositivo) {
        ArrayList<String> puertas= new ArrayList<String>();
        for(int i= 1; i <= 2; i++) {
            DispositivoDibujable d= main.getPiso().getDispositivoDibujable(dispositivo.replace(" ", "")+i);
            if(!d.getDibujar())
                puertas.add(Integer.toString(i));
        }

        String[] puertas_model= new String[puertas.size()];
        for(int i= 0; i < puertas.size(); i++)
            puertas_model[i]= puertas.get(i);

        JComboPuertas.setModel(new javax.swing.DefaultComboBoxModel(puertas_model));
    }
}
